﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommandGraph.Model;
using IDE;
using CommonClasses.Logger;
using EcoExtensions.Model;
using Descriptions_DomainModel.Model;
using Descriptions_DomainModelLoader;

namespace CommandGraphTest
{
    public partial class Form1 : Form
    {
        private ILoggingManagement main_log;
        private ILogger worklog;
        private IDEEcoSpace ecoSpace;

        public Form1()
        {
            main_log = LoggerManager.CreateOnce(".\\LoggerManager.ih.config", ".\\logs");
            worklog = main_log.GetLogger("CommandListsLogger");

            InitializeComponent();

            ecoSpace = new IDEEcoSpace(worklog);
            ecoSpace.Active = true;
            
            graphEditorControl1.Initialize(ecoSpace);

            new DomainModelsLoader(ecoSpace).LoadDeviceDescriptionsFromMainFile(@".\cfg\WorkDomainModel.main.ihdm");

            var domainModel = ecoSpace.Ocl.Evaluate("DomainModel.allInstances->first").AsObject as DomainModel;

            ecoSpace.GetEcoService<CommonClasses.Services.ITranslatorService>()
                .Initialize(domainModel);
            ecoSpace.GetEcoService<CommonClasses.Services.IEditorWithIntelliSense>()
                .Initialize(domainModel);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            CommonClasses.Utility.PathUtility
                .SetCurrentProjectPath(System.IO.Path
                    .GetDirectoryName(Application.ExecutablePath));

            graphEditorControl1.Canvas.Size = new Size(1280, 1024);
            graphEditorControl1.Graph = new CommandsGraph(ecoSpace);
            //textBoxWithIntelliSense1.Initialize(ecoSpace,
            //    ecoSpace.Ocl.Evaluate("DomainModel.allInstances->first").AsObject as DomainModel,
            //    new List<string> { "a", "s" });
            //graphEditorControl1.AddEntity(new Vertex(ecoSpace) 
            //    { 
            //        Rectangle = new EcoRectangle(ecoSpace)
            //        {
            //            Rectangle = new Rectangle(50, 50, 50, 50)
            //        } 
            //    });

            //graphEditorControl1.AddEntity(new Vertex(ecoSpace)
            //{
            //    Rectangle = new EcoRectangle(ecoSpace)
            //    {
            //        Rectangle = new Rectangle(150, 150, 50, 50)
            //    }
            //});
            //graphEditorControl1.AddEntity(new Vertex(ecoSpace)
            //{
            //    Rectangle = new EcoRectangle(ecoSpace)
            //    {
            //        Rectangle = new Rectangle(150, 250, 50, 50)
            //    }
            //});
        }

        private void basicVertexToolStripMenuItem_Click(object sender, EventArgs e)
        {
            graphEditorControl1.ToolBox.ActivateTool("AddVertexTool");
        }

        private void compositeVertexToolStripMenuItem_Click(object sender, EventArgs e)
        {
            graphEditorControl1.ToolBox.ActivateTool("AddCompositeVertexTool");
        }

        private void commentVertexToolStripMenuItem_Click(object sender, EventArgs e)
        {
            graphEditorControl1.ToolBox.ActivateTool("AddCommentVertexTool");
        }

        private void waitVertexToolStripMenuItem_Click(object sender, EventArgs e)
        {
            graphEditorControl1.ToolBox.ActivateTool("AddWaitVertexTool");
        }

        private void ifToolStripMenuItem_Click(object sender, EventArgs e)
        {
            graphEditorControl1.ToolBox.ActivateTool("AddIfVertexTool");
        }

        private void loopVertexToolStripMenuItem_Click(object sender, EventArgs e)
        {
            graphEditorControl1.ToolBox.ActivateTool("AddLoopTool");
        }

        private void startVertexToolStripMenuItem_Click(object sender, EventArgs e)
        {
            graphEditorControl1.ToolBox.ActivateTool("AddStartVertexTool");
        }

        private void finishVertexToolStripMenuItem_Click(object sender, EventArgs e)
        {
            graphEditorControl1.ToolBox.ActivateTool("AddFinishVertexTool");
        }

        private void basicTransitionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            graphEditorControl1.ToolBox.ActivateTool("AddTransitionTool");
        }

        private void commentTransitionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            graphEditorControl1.ToolBox.ActivateTool("AddCommentTransitionTool");
        }

        private void activeTransitionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            graphEditorControl1.ToolBox.ActivateTool("AddActiveTransitionTool");
        }

        private void compileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var code = string.Empty;
            CommandsGraphCompilationUnit unit = null;
            var report = graphEditorControl1.Compile(out unit);
            MessageBox.Show(report.Errors
                .Aggregate("Ошибки компиляции:\nКоличество: " + report.Errors.Count,
                (acc, error) => acc + "\n" + error.ToString())
                + "\n\nКод:\n" + unit.LPMEDCode);
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            (graphEditorControl1.ToolBox.GetToolByName("CommandsGraphLoaderTool") as CommandsGraphLoaderTool)
                .Save(graphEditorControl1.Graph as CommandsGraph, "testFile.cog");
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var prevGraph = graphEditorControl1.Graph;
            graphEditorControl1.Graph = 
                (graphEditorControl1.ToolBox.GetToolByName("CommandsGraphLoaderTool") as CommandsGraphLoaderTool)
                .Load("testFile.cog");

            while (prevGraph.Entities.Count > 0)
                prevGraph.Entities[0].AsIObject().Delete();
            prevGraph.AsIObject().Delete();
        }

    }
}
